package com.example.piece.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.example.base.aop.click.RepeatClick
import com.example.base.ext.navigationTo
import com.example.base.ext.tab_expand.indicator.BaseIndicator
import com.example.base.ext.tab_expand.indicator.LinearIndicator
import com.example.base.ext.tab_expand.tab.*
import com.example.base.ext.tab_expand.text.BaseText
import com.example.base.ui.fragment.BaseFragment
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.FragmentHomeBinding
import com.example.piece.ui.adapter.ViewPagerAdapter
import com.gyf.immersionbar.ktx.immersionBar
import xyz.doikki.videoplayer.player.VideoViewManager

/**
 * 首页fragment
 */

class HomeFragment : BaseFragment<FragmentHomeBinding>() {
    override fun getContentRes(): Int = R.layout.fragment_home


    companion object {
        fun newInstance(): Fragment {
            val fragment = HomeFragment()
            return fragment
        }
    }

    override fun initData(view: View, savedInstanceState: Bundle?) {
        super.initData(view, savedInstanceState)
        val fragments = mutableListOf<Fragment>()
        val titles = listOf("关注", "推荐", "新鲜", "纯文", "趣图")
        fragments.add(FollowFragment.newInstance())
        fragments.add(RecommendationFragment.newInstance())
        fragments.add(FreshFragment.newInstance())
        fragments.add(TextFragment.newInstance())
        fragments.add(PhotoFragment.newInstance())
        mBinding.fragment = this
        mBinding.tab.buildText<BaseText>()
            .setNormalTextBold(true)
            .setSelectTextBold(true)
            .setNormalTextSize(14f)
            .setSelectTextSize(17f)
            .bind()
        mBinding.vp.adapter = ViewPagerAdapter(childFragmentManager, fragments, titles)
        mBinding.tab.setupWithViewPager(mBinding.vp)
        mBinding.tab.fixTabItem(30.toPx())
        mBinding.vp.offscreenPageLimit = 5

    }

    /*跳转搜索页面*/
    @RepeatClick
    fun goToSearch() {
        navigationTo(RoutePath.MAIN_HOME_SEARCH)
    }

    override fun onDetach() {
        super.onDetach()
        getVideoViewManager()?.releaseByTag(Key.DataKey.HOME_LIST)
    }

    /**
     * 子类可通过此方法直接拿到VideoViewManager
     */
    protected fun getVideoViewManager(): VideoViewManager? {
        return VideoViewManager.instance()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            immersionBar {
                fitsSystemWindows(true, com.example.base.R.color.white)
                statusBarDarkFont(true)
                //  statusBarAlpha(0.1f)
            }
        }
    }
}